<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">类目名称</label>
            <div class="layui-input-block">
                <input type="text" name="contents_name" id="contents_name" lay-verify="required"
                       lay-reqtext="用户名不能为空" placeholder="请输入类目名称" value="" class="layui-input">
                <!-- <tip>填写自己管理账号的名称。</tip> -->
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">父目录</label>
            <div class="layui-input-block">
                <select name="contents_level" disabled="disabled" lay-filter="eventName" id="contents_name"
                        class="layui-input newclassname">
                    <!-- <option value="0" selected>请选择父类目</option> -->
                </select>

            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        function getCookie(name) {
            var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
            return r ? r[1] : undefined;
        }

        var contents_id = parent.contents_id;
        var contents_name = parent.contents_name;
        var contents_level = parent.contents_level;
        var contents_parent_id = "0";

        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            $('#contents_name').val(contents_name);
            $(function () {
                $.ajax({
                    type: "post", //get或者post
                    headers: {
                        'X-CSRFToken': getCookie('csrf_token')
                    },
                    url: '/api/v1.0/contents_parent_list',
                    data: {}, //参数 接口传参 json对象
                    success: function (res) {
                        console.log(contents_id);
                        console.log(contents_name);
                        console.log(contents_level);
                        console.log(res.data);
                        if (res.data) {
                            var optionHtml = '';
                            for (var i in res.data) {
                                console.log(res.data[i]['contents_name']);
                                // if (res.data[i]['contents_name'] == contents_level) {
                                // 	console.log("进入if判断");
                                // }
                                if (contents_level == '0') {
                                    console.log("contents_level是0");
                                    optionHtml = '<option value="0" selected>请选择父类目</option>'
                                    $('.newclassname').append(optionHtml);
                                    console.log(optionHtml);
                                    layui.form.render('select');
                                    return;
                                } else if (contents_level == res.data[i]['contents_name']) {
                                    console.log("contents_level不是0");
                                    // console.log(contents_level);
                                    // console.log(res.data[i]['contents_name']);
                                    // console.log(res.data[i]['contents_id']);
                                    contents_parent_id = res.data[i]['contents_id'];
                                    var value2 = res.data[i]['contents_name'];
                                    optionHtml = "<option value='" + contents_parent_id +
                                        "' selected>" +
                                        value2 + "</option>";
                                    console.log(optionHtml);
                                    $('.newclassname').append(optionHtml);
                                    layui.form.render('select');
                                    return;
                                }
                            }
                            ;

                        }
                    }
                });
            })

            //监听提交
            form.on('submit(saveBtn)', function () {
                console.log(contents_parent_id);
                var data = {
                    "contents_id": contents_id,
                    "contents_name": $("#contents_name").val(),
                    "contents_level": contents_parent_id
                }
                // data['contents_id'] = contents_parent_id;
                $.ajax({
                    type: "post", //get或者post
                    contentType: 'application/json',
                    headers: {
                        'X-CSRFToken': getCookie('csrf_token')
                    },
                    url: '/api/v1.0/edit_contents',
                    data: JSON.stringify(data), //参数 接口传参 json对象
                    success: function (res) {
                        var index = layer.alert("保存成功", {
                            title: '操作'
                        }, function () {
                            // 关闭弹出层
                            layer.close(index);
                            // 刷新列表页
                            window.parent.location.reload()
                        });
                    }
                });

                return false;
            });

        });
    </script>
</body>
</html>
